// /////////////////////////////////////////////////////////////////////////////
//
// Name:            DataStore.cpp
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           The DataStore class manages the DataStacks that are being
//                  used by the framework.
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

// Include files
#include "DataStore.h"


// DataStore class
// DataStore constructor.
DataStore::DataStore()
{
    init();
}

// DataStore deconstructor.
DataStore::~DataStore()
{
    clear();
}

// Initialises the DataStore.
void DataStore::init()
{
}

// Clears the DataStore.
void DataStore::clear()
{
    removeDataStacks();
}

// Increases the reference counter.
void DataStore::grab()
{
    // Dummy function.
}

// Decreases the reference counter.
void DataStore::drop()
{
    // Dummy function.
}

// Adds the given DataStack to the DataStore.
bool DataStore::addDataStack(DataStack *stack)
{
    // Did we get a valid pointer?
    if(stack == NULL)
      return false;

    // Does a DataStack with the same name exist?
    if(getDataStack(stack->getName()))
      return false;

    // Add the DataStack to the vector.
    mStacks.push_back(stack);
    stack->grab();

    return true;
}

// Creates (and adds) a DataStack with the given name.
DataStack* DataStore::createDataStack(const std::string &name, bool grab)
{
    // Does a Data Stack with the same name exist?
    if(grab && getDataStack(name))
      return NULL;

    // Create the DataStack and add it to the vector.
    DataStack *stack = new DataStack(name);

    if(grab)
    {
       mStacks.push_back(stack);
       stack->grab();
    }

    return stack;
}

// Gets the DataStack with the given ID.
DataStack* DataStore::getDataStack(const u32 id)
{
    for(u32 i = 0; i < mStacks.size(); i++)
    {
       if(mStacks[i]->getID() == id)
         return mStacks[i];
    }

    return NULL;
}

// Gets the DataStack with the given name.
DataStack* DataStore::getDataStack(const std::string &name)
{
    for(u32 i = 0; i < mStacks.size(); i++)
    {
       if(mStacks[i]->getName() == name)
         return mStacks[i];
    }

    return NULL;
}

// Removes all DataStacks from this DataStore.
void DataStore::removeDataStacks()
{
    // Clear the DataStacks.
    for(u32 i = 0; i < mStacks.size(); i++)
       mStacks[i]->drop();

    mStacks.clear();
}

// Removes the given DataStack.
bool DataStore::removeDataStack(DataStack *stack)
{
    // Did we get a valid pointer?
    if(stack == NULL)
      return false;

    // Try to remove the DataStack.
    vector<DataStack*>::iterator it;

    for(it = mStacks.begin(); it < mStacks.end(); it++)
    {
        DataStack *dstack = *it;

        if(dstack == stack)
        {
           dstack->drop();
           mStacks.erase(it);
           return true;
        }
    }

    // We couldn't find the DataStack and thus couldn't remove it.
    return false;
}

// Removes the given DataStack with the given ID.
bool DataStore::removeDataStack(const u32 id)
{
    // Try to remove the DataStack.
    vector<DataStack*>::iterator it;

    for(it = mStacks.begin(); it < mStacks.end(); it++)
    {
        DataStack *dstack = *it;

        if(dstack->getID() == id)
        {
           dstack->drop();
           mStacks.erase(it);
           return true;
        }
    }

    // We couldn't find the DataStack and thus couldn't remove it.
    return false;
}

// Removes the given DataStack with the given name.
bool DataStore::removeDataStack(const std::string &name)
{
    // Try to remove the DataStack.
    vector<DataStack*>::iterator it;

    for(it = mStacks.begin(); it < mStacks.end(); it++)
    {
        DataStack *dstack = *it;

        if(dstack->getName() == name)
        {
           dstack->drop();
           mStacks.erase(it);
           return true;
        }
    }

    // We couldn't find the DataStack and thus couldn't remove it.
    return false;
}

// End of File
